package com.lightjet.macross.base.upms.controller;

import com.lightjet.macross.base.upms.domain.ResourceInfo;
import com.lightjet.macross.base.upms.domain.RoleInfo;
import com.lightjet.macross.base.upms.domain.RoleResourceLink;
import com.lightjet.macross.base.upms.service.RoleResourceLinkService;
import com.lightjet.macross.common.dto.ResponseDTO;
import com.lightjet.macross.common.util.RespUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.java.Log;
import org.checkerframework.checker.guieffect.qual.PolyUIType;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api/roleresource")
@Scope("request")
@Log
@Api(tags="通用系统权限管理-角色资源相关接口集")
public class RoleResourceController {

    @Resource
    private RoleResourceLinkService roleResourceLinkService;

    @GetMapping("/list")
    @ApiOperation(value = "系统角色资源查询接口", notes = "根据角色ID获取该角色拥有的资源集合")
    public ResponseDTO<List<ResourceInfo>> GetByRoleId(@ApiParam(name="roleid",value = "角色ID",required = true) Integer roleid)
    {
        List<ResourceInfo> list = roleResourceLinkService.FindByRoleinfo(new RoleInfo(roleid)).stream()
                .map(RoleResourceLink::getResourceInfo).collect(Collectors.toList());
        list.forEach(n->n.setChildren(null));
        return RespUtil.CreateOK(list);
    }

    @PutMapping("/list")
    @ApiOperation(value = "系统角色资源保存接口",notes = "根据角色ID和资源列表Json（平级列表）更新角色拥有资源信息")
    public ResponseDTO<String> UpdateResources(
            @ApiParam(name="resources",value = "资源列表Json",required = true) @RequestBody List<ResourceInfo> resources,
            @ApiParam(name="roleid",value = "角色ID",required = true) Integer roleid
            )
    {
        return roleResourceLinkService.UpdateResources(new RoleInfo(roleid), resources);
    }
}
